package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.dto.DtsPermissionDto;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsRoleService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AdminResponseCode;
import com.itqf.utils.AdminResponseUtil;
import com.itqf.utils.Permission;
import com.itqf.utils.PermissionUtil;
import com.itqf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/13 17:18
 * description:角色controller
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/role/")
public class AdminRoleController {

    @Autowired
    private DtsRoleService dtsRoleService;

    public static final Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @RequiresAuthentication
    @GetMapping("options")
    public Object options() {

        logger.info("[角色列表]接口开始调用!");

        List<Map> roleList = dtsRoleService.findRoleNames();

        logger.debug("[角色列表]查询数据为:{}", roleList);

        logger.info("[角色列表]接口结束调用!");

        return ResponseUtil.ok(roleList);
    }


    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色查看")
    @GetMapping("list")
    public Object list(DtsSearchDto searchDto) {

        logger.info("[角色管理]列表查询接口调用!传入的参数为:{}", searchDto);

        Map data = dtsRoleService.queryRoleByPage(searchDto);

        logger.info("[角色管理]列表查询接口调用结束!查询的结果为:{}", data);

        return ResponseUtil.ok(data);
    }

    @Autowired
    public ApplicationContext applicationContext;

    @RequiresPermissions("admin:permission:show")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "权限展示")
    @GetMapping("permissions")
    public Object show(int roleId) {

        logger.info("[权限读取接口]开始调用!查询权限的角色id为:{}", roleId);

        if (roleId == 0) {
            logger.warn("[权限读取接口]没有传递角色id!无法查询该角色拥有的权限!");
            return ResponseUtil.fail();
        }

        //1.查询程序拥有的全部权限[程序拥有的权限指的是已经写完的接口]
        //查询已经写好的全部接口
        List<Permission> permissionList =
                PermissionUtil.listPermission(applicationContext, "com.itqf.controller");
        //处理结果集,变成层次展示类型 systemPermissions
        List<PermVo> permVos = PermissionUtil.listPermVo(permissionList);

        //2.查询用户拥有的全部权限
        //判断是否是超级管理员
        boolean isSuper = dtsRoleService.checkSuper(roleId);
        //当前用户拥有权限的集合
        Set<String> assignedPermissions = new HashSet<>();
        if (isSuper) {
            for (Permission permission : permissionList) {
                //读取权限权限注解的value值
                assignedPermissions.add(permission.getRequiresPermissions().value()[0]);
            }
        }else{
            assignedPermissions = dtsRoleService.queryPermissions(roleId);
        }

        //3.结果集封装
        Map data = new HashMap();
        data.put("systemPermissions",permVos);
        data.put("assignedPermissions",assignedPermissions);

        logger.info("[权限读取接口]数据读取成功!最终数据为:{}", data);

        return  ResponseUtil.ok(data);
    }


    @RequiresPermissions("admin:permission:update")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"}, button="权限修改")
    @PostMapping("permissions")
    public Object update(@RequestBody DtsPermissionDto permissionDto){

        //1.参数校验
        if (permissionDto.getRoleId() == 0) {
            logger.error("[权限修改]传入的roleid为0,出现异常!");
            return ResponseUtil.badArgument();
        }
        //2.清空所有[注意超级管理员权限不可以改变]

        boolean isSuper = dtsRoleService.checkSuper(permissionDto.getRoleId());

        if (isSuper) {

            logger.warn("[权限修改]要修改的角色id:{},是超级管理员,没法修改!",permissionDto.getRoleId());

            return AdminResponseUtil.fail(AdminResponseCode.ROLE_SUPER_SUPERMISSION);
        }

        dtsRoleService.removePermission(permissionDto.getRoleId());

        //3.添加新的权限即可

        dtsRoleService.save(permissionDto);

        return ResponseUtil.ok(null);
    }


}
